/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.nota.vista;

import basedatos.OperacionesBD;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import modulos.nota.modelo.Nota;
import modulos.usuario.modelo.Usuario;
import vista.frmPrincipal;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author francisco
 */
public class FrmListaNotas extends javax.swing.JInternalFrame implements ListSelectionListener {

    /**
     * Creates new form FrmListaNotas
     */
    public FrmListaNotas() {
        initComponents();
        OperacionesVarias.centrar(this);
        listar();
        if (!notas.isEmpty()) {
            poneTabla(notas);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblSugerencias = new javax.swing.JTable();
        tblSugerencias.getSelectionModel().addListSelectionListener(this);
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        textoDetalle = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("NOTAS");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Edit.png"))); // NOI18N
        setVisible(true);

        jScrollPane1.setViewportView(tblSugerencias);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        textoDetalle.setColumns(20);
        textoDetalle.setLineWrap(true);
        textoDetalle.setRows(5);
        jScrollPane2.setViewportView(textoDetalle);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        jButton2.setText("Nuevo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        jButton3.setText("Eliminar");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Recibidas", "Enviadas" }));

        jLabel1.setText("Nota:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 570, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 570, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(272, 272, 272)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(30, 30, 30)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-619)/2, (screenSize.height-423)/2, 619, 423);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
 
        
                FrmNotas Fn = new FrmNotas();
                frmPrincipal.dskpPrincipal.add(Fn);
                Fn.toFront();
                Fn.setVisible(true);
        
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblSugerencias;
    private javax.swing.JTextArea textoDetalle;
    // End of variables declaration//GEN-END:variables
    private ArrayList<Nota> notas;
    Usuario logueado=(Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
    private void listar() {        
        notas = (ArrayList<Nota>) OperacionesBD.listarPorUsuario("Nota", logueado);
    }

    private void poneTabla(ArrayList<Nota> lista) {
        if (notas != null) {
            DefaultTableModel dtm = new DefaultTableModel();
            dtm.addColumn("Titulo");
            dtm.addColumn("Fecha");
            for (Object o : notas) {
                Nota s = (Nota) o;
                Object[] obj = new Object[2];
                obj[0] = s.getTitulo();
                obj[1] = OperacionesVarias.fechaString(s.getFecha().getTime());
                dtm.addRow(obj);
            }
            tblSugerencias.setModel(dtm);
        }

    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        int row = tblSugerencias.getSelectedRow();
        if (row >= 0) {
            Nota s = (Nota) notas.get(row);
            textoDetalle.setText(s.getDescripcion());
        }
    }
}
